<?php

class Foto extends CActiveRecord {
    /**
     * The followings are the available columns in table 'z_foto':
     * @var integer $id_foto
     * @var integer $id_foto_cat
     * @var string $name
     * @var string $description
     * @var string $link
     * @var string $c_date
     * @var string $u_date
     * @var string $autor
     */
    public $file_name;
    private $counts;
    /**
     * Returns the static model of the specified AR class.
     * @return CActiveRecord the static model class
     */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    public function behaviors() {
        return array(
                'AutoTimestampBehavior' => array(
                        'class' => 'application.components.AutoTimestampBehavior',
                // вы можете указать дополнительные опции здесь
                )
        );
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'z_foto';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
                array('id_foto_cat, name, description, link, autor', 'required'),
                array('id_foto_cat', 'numerical', 'integerOnly'=>true),
                array('c_date, u_date', 'safe'),
                // The following rule is used by search().
                // Please remove those attributes that should not be searched.
                array('id_foto, id_foto_cat, name, description, link, c_date, u_date, autor', 'safe', 'on'=>'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }
    public static function backImage($id, $id_cat) {
        $criteria=new CDbCriteria;
        $criteria->condition='id_foto_cat=:postID AND id_foto<:foto';
        $criteria->params=array(':postID'=>$id_cat, ':foto'=>$id);

        $view = Foto::model()->FindAll(    $criteria    );

        $end = end($view);
        if(!$end->id_foto) {
            $foto = Foto::model()->findAll(
                    array(
                    'condition'=>'id_foto_cat=:postID',
                    'params'=>array(':postID'=>$id_cat),
                    )
            );
            return end($foto)->id_foto;
        }
        return $end->id_foto;
    }
    public static function countes($id, $id_cat) {

        $count= Foto::model()->count(array(
                'condition'=>'id_foto_cat=:postID',
                'params'=>array(':postID'=>$id_cat),
        ));
        $view = Foto::model()->FindAll(
                array(
                'condition'=>'id_foto_cat=:postID',
                'params'=>array(':postID'=>$id_cat),
                )
        );
        for($a = 0;$a < count($view); $a ++ )
            if($id == $view[$a]->id_foto) return ++$a.' фотография в альбоме из '.$count;

    }

    public static function nextImage($id, $id_cat) {
        $criteria=new CDbCriteria;
        $criteria->condition='id_foto_cat=:postID';
        $criteria->params=array(':postID'=>$id_cat);

        $view = Foto::model()->FindAll(    $criteria    );
        for($a = 0, $b = 0;$a < count($view); $a ++ ) {
            if($b) return $view[$a]->id_foto;
            if($id == $view[$a]->id_foto) {
                if($a == count($view) - 1) return $view[0]->id_foto;
                else $b++;

            }
        }

    }

    /*
         * @overload delete comments
    
    public function afterDelete() {
        Coments::model()->deleteAll(
                array(
                'condition'=>'id_name=:id_name AND id_post=:id_post',
                'params'=>array(
                        ':id_name'=>'foto',
                        ':id_post'=>$this->id_foto),
        ));
    }
*/
    public function getAllCat() {
        $temps = FotoC::model()->findAll();
        foreach($temps as $temp) {
            $tempArray[$temp->id_foto_cat] = $temp->name;
        }
        return $tempArray;
    }

    public function getName() {
        $model = FotoC::model()->findByPk($id);
        return $model->name;
    }

    public function cout3Foto() {
        static $a = 3;
        return $a++;
    }

    public function setCounts($id_cat) {
        $this->counts =  Foto::model()->count(array(
                'condition'=>'id_foto_cat=:postID',
                'params'=>array(':postID'=>$id_cat),
        ));
    }

    public function getCounts() {
        return $this->counts;
    }
    /**
     /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
                'id_foto' => 'id',
                'id_foto_cat' => 'Категория',
                'name' => 'Название',
                'description' => 'Описание',
                'link' => 'Ссылка',
                'c_date' => 'Дата создания',
                'u_date' => 'Дата изменения',
                'autor' => 'Автор',
        );
    }
    protected function afterDelete() {
        parent::afterDelete();
        Coment::model()->deleteAll('id_post='.$this->id_post.' AND id_name = "foto"');
        PostTag::model()->updateFrequency($this->tags, '');
    }
    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;

        $criteria->compare('id_foto',$this->id_foto);

        $criteria->compare('id_foto_cat',$this->id_foto_cat);

        $criteria->compare('name',$this->name,true);

        $criteria->compare('description',$this->description,true);

        $criteria->compare('link',$this->link,true);

        $criteria->compare('c_date',$this->c_date,true);

        $criteria->compare('u_date',$this->u_date,true);

        $criteria->compare('autor',$this->autor,true);

        return new CActiveDataProvider('Foto', array(
                        'criteria'=>$criteria,
        ));
    }
}